package marvin.webapp.audio.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConfigReader {
	private static final String MARVIN_CONF_FILE_NAME = "marvin.conf";
	public static final String ROOT_FILES_KEY = "ROOT_FILES_KEY";
	public static final String WEB_RADIOS_KEY = "WEB_RADIOS_KEY";

	private static String configFilePath = System.getenv("MARVIN_CONFIG");
	private static Map<String, List<String>> configMap;
	
	public static String CONFIG_FILE_ABSOLUTE_PATH = configFilePath+File.separatorChar+MARVIN_CONF_FILE_NAME;
	
	public static List<String> getParameters(String key){
		if(configMap == null){
			readConfiguration();
		}
		
		List<String> result = new ArrayList<String>();
		
		if(configMap.containsKey(key))
			result = configMap.get(key);
		
		return result;
	}
	
	public static void setParameters(String key, List<String> values){
		if(configMap == null){
			readConfiguration();
		}
		
		configMap.put(key, values);
	}
	
	public static void readConfiguration(){
		System.out.println("Try to read $MARVIN_CONFIG/marvin.conf file ("+configFilePath+")");
		File conf = new File(configFilePath+File.separatorChar+MARVIN_CONF_FILE_NAME);
		
		int rootFileIndex = 0, radioIndex = 0, current = 0;
		
		List<String> rootFiles = new ArrayList<String>();
		List<String> webRadios = new ArrayList<String>();
		
		if(conf.exists()){	
			try{
				InputStream ips=new FileInputStream(conf); 
				InputStreamReader ipsr=new InputStreamReader(ips);
				BufferedReader br=new BufferedReader(ipsr);
				String ligne;
				while ((ligne=br.readLine())!=null){
					current++;
					System.out.println(ligne);
					if(!ligne.startsWith("#") && ! ligne.isEmpty()){
						if(ligne.startsWith("Root files:")){
							rootFileIndex = current;
						} else if(ligne.startsWith("Web radio URLs:")){
							radioIndex = current;
						} else {
							if(current > rootFileIndex && radioIndex == 0)
								rootFiles.add(ligne);
							else if (current > radioIndex)
								webRadios.add(ligne);
						}
					}
				}
				br.close(); 
			}catch (Exception e){
				System.out.println(e.toString());
			}			
		}
		configMap = new HashMap<String, List<String>>();
		configMap.put(ROOT_FILES_KEY, rootFiles);
		configMap.put(WEB_RADIOS_KEY, webRadios);
	}
	
	public static boolean saveConfiguration(String filePath){
		boolean saved = false;
		if(filePath != null && !filePath.isEmpty()){
			String lineSeparator = System.lineSeparator();
			String configuration = "# List elements that are considered as 'root' elements in the tree" + lineSeparator;
			
			// Files
			configuration += "Root files:" + lineSeparator;
			List<String> files = configMap.get(ROOT_FILES_KEY);
			if(files != null){
				for (String file : files) {
					configuration += file + lineSeparator;
				}
			}
			
			configuration += lineSeparator;
			
			// Web radios
			configuration += "#Web radio URLs coming from http://www.linuxpedia.fr/doku.php/flux_radio" + lineSeparator;
			configuration += "Web radio URLs:" + lineSeparator;
	
			List<String> radios = configMap.get(WEB_RADIOS_KEY);
			if(radios != null){
				for (String radio : radios) {
					configuration += radio + lineSeparator;
				}
			}
			
			File dstFile = new File(filePath);
			OutputStream out;
			try {
				out = new FileOutputStream(dstFile);
				out.write(configuration.getBytes());
				out.close();
				saved = true;
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
		return saved;
	}
	
	public static void main(String[] args) {
		ConfigReader.readConfiguration();
		boolean saved = ConfigReader.saveConfiguration(configFilePath+File.separatorChar+MARVIN_CONF_FILE_NAME);
		
		System.out.println("Configuration saved: "+saved);
	}
}
